﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;
using ByteSite.DBUtility;
using ByteSite.Common;
using SqlSugar;
using System.Linq.Expressions;

namespace ByteSite.DAL
{
    /// <summary>
    /// 数据访问类:扩展字段表
    /// </summary>
    public partial class form_attribute_field
    {
        public SqlSugarClient db;
        private string databaseprefix;//数据库表名前缀
        public form_attribute_field(string _databaseprefix)
        {
            databaseprefix = _databaseprefix;
            db = new DBUtility.DbContext().Db;
        }

        /// <summary>
        /// 获得频道对应的数据
        /// </summary>
        public List<Entitys.dt_form_attribute_field> GetList(int form_id, Expression<Func<Entitys.dt_form_attribute_field, Entitys.dt_form_field, bool>> whereExpression)
        {
            return db.Queryable<Entitys.dt_form_attribute_field, Entitys.dt_form_field>((a, s) => new JoinQueryInfos(JoinType.Inner, a.id == s.field_id))
                .Where((a, s) => s.form_id == form_id)
                .Where(whereExpression)
                .OrderBy((a, s) => a.is_sys, OrderByType.Desc)
                .OrderBy((a, s) => a.sort_id, OrderByType.Asc)
                .OrderBy((a, s) => a.id, OrderByType.Desc)
                .ToList();
        }

        /// <summary>
        /// 获取频道的扩展字段
        /// </summary>
        public Dictionary<string, string> GetFields(int form_id)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            List<Entitys.dt_form_attribute_field> list = GetList(form_id, (a, s) => true);
            if (list.Count > 0)
            {
                foreach (Entitys.dt_form_attribute_field f in list)
                {
                    dic.Add(f.name.ToString(), string.Empty);
                }
            }
            return dic;
        }
    }
}